<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo 用户注册引导
 *@link www.zeevin.com
 */
class RegController extends BaseController{
	public $layout='';
	private $avatar_dir;

	public function init(){
		parent::init();
		$this->avatar_dir = Yii::app()->basePath.'/../attachment/avatar/';
	}

	public function actionTags(){
		$type = $_GET['type']?1:0;
		$msg['status']=0;
		$model = new UserTages('create');

		$hot_tags = Tags::getHot();
		$forword_tags = Tags::getForword();
		// print_r($hot_tags);
		$rs = Yii::app()->db->createCommand()
		->select('a.id,a.tag_id,b.tag_name')
		->from('user_tags a')
		->leftJoin('tags b','a.tag_id=b.id')
		->where('a.uid=? and a.type=?',array($this->uid,$type))
		->query();

		if(isset($_POST['UserTages']['taglist'])){
			// print_r($type);exit;
			$model->attributes = $_POST['UserTages'];
			if($model->validate()){
				$tmp = $_POST['UserTages']['taglist'];
				if(strpos($tmp, ',') || strpos($tmp, ',')){
					$tags = str_replace('，', ',', $tmp);
					$tags = explode(',', $tags);
				}else $tags = array($tmp);

				$all_num = count($tags)+$rs->count();
				if($all_num>20){
					$msg['status']=1;
					$msg['desc']="您已经有".$rs->count()."个标签,本次还能添加最多".(20-$rs->count()).'个';
				}else{
					$transcation = Yii::app()->db->beginTransaction();
					try {
						foreach($tags as $value){
							if(!$value)continue;
							//查看此标签是否已经存在标签基表里
							$tag = Tags::model()->findByAttributes(array('tag_name'=>$value));
							if($tag){
								$tag->hot = $tag->hot+1;
							}else{
								$tag = new Tags;
								$value = trim($value);
								$tag->tag_name = $value;
							}
							if($tag->save()){
								$model = new UserTages('create');
								$model->uid = $this->uid;
								$model->tag_id = $tag->id;
								$model->ctime = date('Y-m-d H:i:s');
								$model->type = $type;
								$model->insert();
							}
							
						}
						$transcation->commit();
						$this->refresh();
						
						
					} catch (Exception $e) {
						$transcation->rollback();
						$msg['status']=1;
					}
				}
			}
		}
		$this->render('tags',array('rs'=>$rs,'model'=>$model,'msg'=>$msg,'hot_tags'=>$hot_tags,'forword_tags'=>$forword_tags));
	}

	public function actionIntro(){
		$split = 'zichash';
		$drop_split='drop_del';
		$intro = Profile::model()->findByAttributes(array('uid'=>$this->uid))?:(new Profile('create'));
		$hook = ProfileHook::model()->findAllByAttributes(array('uid'=>$this->uid))?:(new ProfileHook('create'));
		if(Yii::app()->request->isAjaxRequest){
			ProfileHook::model()->deleteAllByAttributes(array('uid'=>$this->uid));
			if(!$intro->isNewRecord)$intro->delete();
			$intro = new Profile('create');
			if(count($_POST)){
				$status = true;
				if(count($_POST['Profile'])){
					$intro->attributes = $_POST['Profile'];
					$status = $intro->save();
				}
				if($status){

					foreach((array)$_POST['Hook'] as $k=>$v){
						$hook = new ProfileHook('create');
						$hook->subject = trim($k);
						$hook->content = trim($v);
						$hook->save();
					}
				}
				// $this->redirect('?r=search');
				echo $status;exit;

				

			}
		}
		$this->render('intro',array('intro'=>$intro,'hook'=>$hook,'split'=>$split,'drop_split'=>$drop_split));
	}

	public function actionAvatar(){
		$model = User::model()->findByPk($this->uid);
		// var_dump($model);
		if(Yii::app()->request->isAjaxRequest && $model->avatar){
			unset($_GET['r']);
			if(!empty($_GET)){
				if($model->avatar=='avatar.jpg'){
					$msg['status']=1;
					$msg['desc']='请先上传一张自已的图片再剪切';
				}else{
					$width = $_GET['width'];
					$height = $_GET['height'];
					$thumb = Utils::genThumb(Utils::getAvatar($model->avatar),$_GET['x1'],$_GET['y1'],$width,$height);
					if($thumb){
						$msg['status']=0;
						$msg['desc']='头像缩略图保存成功';
					}else{
						$msg['status']=1;
						$msg['desc']='头像缩略图保存失败';
					}
				}
				
				print_r(json_encode($msg));exit;
			}
			// print_r($_GET);exit;

		}

		if($_POST){
			$avatar = CUploadedFile::getInstance($model,'avatar');

			$hash_name = $this->uid.'-'.substr(time(),6).rand(100,999).'.'.$avatar->extensionName;

			$model->avatar = $hash_name;
			$model->password_tmp = 'hashhash';
			// echo $hash_name;
			if(!empty($avatar) && $model->save()){
				// var_dump($avatar);
				file_exists($this->avatar_dir)?null:mkdir($this->avatar_dir);
				if($avatar->saveAs($this->avatar_dir.$hash_name)){
					$msg['status']=0;
					$msg['desc']='图片上传成功';
					Utils::resizeAvatar($this->avatar_dir.$hash_name);
					$this->refresh();
				}else{
					$msg['status']=1;
					$msg['desc']='图片上传失败：'.$avatar->error;

				}
			}else{
				// print_r($model->errors);
				$msg['status']=1;
			}
		}
		$this->render('avatar',array('model'=>$model));
	}

	public function actionProfile(){
		$model = UserIntro::model()->find('uid=?',array($this->uid))?:(new UserIntro('insert'));

		if(isset($_POST['UserIntro'])){
			$model->attributes = $_POST['UserIntro'];
			if($model->save()){
				$msg['status']=0;
				$msg['desc']='个人介绍设置成功';
				$this->redirect('?r=reg/intro');
			}
		}
		$this->render('profile',array('model'=>$model,'msg'=>$msg));
	}

}